#!/bin/sh
if [ $# -ne 2 ]
then
        echo "+----------------------------------------------+"
        echo "| versionDiff.sh <newImage> <oldImage>         |"
        echo "+----------------------------------------------+"
        return 1
fi
TARPATH=/home/xjy/local_update/version/tar/
newImage=$1
oldImage=$2
mkdir $newImage
mkdir $oldImage
cd $newImage
tar -xvf $TARPATH$newImage
if [ $? -ne 0 ];then
	return -1 
fi
cd ../$oldImage
tar -xvf $TARPATH$oldImage
if [ $? -ne 0 ];then
	return -1 
fi
cd ..
#diff -qrN $newImage $oldImage > diff.tmp
diff -qr $newImage $oldImage > diff.tmp

cat diff.tmp|grep "^Files"|awk -F ' ' '{print $2}'|sort > diffSort.tmp
#cat diff.tmp|grep "^Only in"|awk -F ': ' '{print $1}'|awk -F ' ' '{print $3}' > onlyHead.tmp
#cat diff.tmp|grep "^Only in"|awk -F ': ' '{print $2}' > onlyTail.tmp

cat diff.tmp| while read line
do
	head=
	tail=
	head=`echo $line|grep "^Only in"|grep $newImage|awk -F ': ' '{print $1}'|awk -F ' ' '{print $3}'`
	tail=`echo $line|grep "^Only in"|grep $newImage|awk -F ': ' '{print $2}'`
	if [ ! -n "$head" ] || [ ! -n "$tail" ]; then  
		continue
	else  			
		fpath=$head/$tail
		if [ -d $fpath ];
		then
			echo "$fpath is a direstory.";
			filelist=`ls $fpath`
			for filename in $filelist
			do	
				echo "$fpath+++$filename"
				echo "$fpath/$filename" >> diffSort.tmp
			done
		elif [ -e $fpath ];
		then    
			echo "$fpath is a file.";
		else
			echo "$fpath is NOT a file or direstory.";
		fi		
#		echo "$head/$tail" >> diffSort.tmp
	fi
done

cd $newImage
tar -cvf link.tar `find ./ -type l`
#lfile=`find ./ -type l`
echo "$newImage/link.tar" >> ../diffSort.tmp
cd ..
cp diffSort.tmp update.xml
rm -rf *.tmp
#rm -rf $newImage
#rm -rf $oldImage

